package com.android.friend.lib.network.httptask;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.apache.http.NameValuePair;

import android.content.Context;
import android.util.Log;

import com.android.friend.app.R;
import com.android.friend.lib.dialog.util.DialogUtil;
import com.android.friend.lib.network.handler.JsonPaserExceptions;
import com.android.friend.lib.network.util.HttpUtil;
import com.android.friend.lib.network.util.JsonUtil;

public abstract class HttpPostStringTask<Param, Result> extends
		MobiNetworkTask<Param, Void, Result> {

	private Class<Result> mType;
	// get parameter type from generic class
	// in class return type is result type
	final int indexResultType = 1;

	private List<NameValuePair> mLValuePairs;

	public HttpPostStringTask(Context pContext, boolean pShowLoading,
			List<NameValuePair> pValuePairs) {
		super(pContext, pShowLoading);
		this.mType = this.getType();
		this.mLValuePairs = pValuePairs;
	}

	public HttpPostStringTask(Context pContext, List<NameValuePair> pValuePairs) {
		super(pContext, false);
		this.mType = this.getType();
		this.mLValuePairs = pValuePairs;
	}

	private Class<Result> getType() {
		@SuppressWarnings("unchecked")
		Class<Result> classType = (Class<Result>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[indexResultType];
		return classType;
	}
	

	protected String getStr(int id) {
		return mContext.get().getResources().getString(id);
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.smartebook.android.mobibook.networkhandler.networktask.MobiNetworkTask
	 * #processInBackground (Params[])
	 */
	@Override
	public Result processInBackground(Param... pParams) throws IOException {
		final String json = HttpUtil.getStringResponeByPostMethod(
				(String) pParams[0], mLValuePairs);
		Log.i("MobiNetworkJsonTask", " Json :" + json);
		Result result = null;
		try {
			result = JsonUtil.fromString(json, mType);
		} catch (JsonPaserExceptions e) {
			e.printStackTrace();
			onJsonPaserException(e, mContext.get(), pParams);
		}
		return result;
	}
	
	@Override
	protected void onConnectionTimeout(IOException exception, Context pContext,Param... params) {
		super.onConnectionTimeout(exception, pContext, params);
//		DialogUtil.showAlerOkbutton(mContext.get(), getStr(R.string.infor), getStr(R.string.connection_timeout));
	}
	
	@Override
	protected void onSocketTimeout(IOException exception, Context pContext,Param... params) {
		super.onSocketTimeout(exception, pContext, params);
//		DialogUtil.showAlerOkbutton(mContext.get(), getStr(R.string.infor), getStr(R.string.connection_timeout));
	}
	
	@Override
	protected void onNoNetworkConnected(Context pContext, Param... params) {
		super.onNoNetworkConnected(pContext, params);
		DialogUtil.showConfirmDialog(mContext.get(), "Message", "Network is not ready");
	}

	protected void onJsonPaserException(Exception exception, Context pContext,
			Param... param) {

	}

}
